/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javabeans.Event;
import javabeans.GroupPlan;
import javabeans.Plan;

/**
 *
 * @author zhi
 */
public class FinalScheduleServiceImpl implements FinalScheduleService{

    public void sortEventList(List<Event> list) {
        EventComparator comp = new EventComparator();
        Collections.sort(list, comp);
    }
}

class EventComparator implements Comparator {

    public int compare(Object o1, Object o2) {
        // TODO Auto-generated method stub
        Event e1 = (Event) o1;
        Event e2 = (Event) o2;
        int result = 0;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = e1.getEventdate();
        String date2 = e2.getEventdate();
        try {
            Date d1 = (Date) dateFormat.parse(date1);
            Date d2 = (Date) dateFormat.parse(date2);
            if(d1.after(d2) == true) {
                result = 1;
            } else if(d1.before(d2) == true) {
                result = -1;
            } else {  //date is the same, compare time
                String time1 = e1.getEventtime();
                String time2 = e2.getEventtime();
                dateFormat = new SimpleDateFormat("HH:mm");
                Date t1 = (Date) dateFormat.parse(time1);
                Date t2 = (Date) dateFormat.parse(time2);
                if(t1.after(t2)) result = 1;
                else if(t1.before(t2)) result = -1;
                else result = 0;
            }
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
        return result;
    }
}
